package com.base.data
{
	import com.base.data.account.AccountBean;
	import com.base.data.message.MessageBean;
	import com.base.data.sendrule.SendRuleBean;
	import com.base.util.Utils;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;

	/**
	 * 文件操作类 
	 * @author Administrator
	 */	
	public class File 
	{
		/**
		 *用户类型
		 */		
		public static var File_Type_Account:uint = 1;
		/**
		 *帖子类型
		 */		
		public static var File_Type_Message:uint = 2;
		/**
		 *发帖规则 
		 */		
		public static var File_Type_Rule:uint = 3;
		
		/**
		 *用户文件名 
		 */		
		public static var File_Name_Account:String = "data\\act.dat";
		/**
		 *帖子文件名 
		 */		
		public static var File_Name_Message:String = "data\\msg.dat";
		/**
		 *发帖规则名 
		 */		
		public static var File_Name_Rule:String = "data\\rule.dat";
		
		public function File()
		{
			
		}
		
		/**
		 * 获取文件路径 
		 * @param type 文件类型
		 * @return 
		 */		
		private static function getFileUrl( type:uint ):String {
			if( type == File_Type_Account ) return  flash.filesystem.File.applicationDirectory.nativePath +"\\"+ File_Name_Account;
			else if( type == File_Type_Message ) return  flash.filesystem.File.applicationDirectory.nativePath +"\\"+ File_Name_Message;
			else if( type == File_Type_Rule ) return  flash.filesystem.File.applicationDirectory.nativePath +"\\"+ File_Name_Rule;
			else {
				throw new Error("请传入正确文件类型");
				return null;
			}
		}
		
		private static function getObjClass( type:uint ):Object {
			if( type == File_Type_Account ) return new AccountBean();
			else if( type == File_Type_Message ) return new MessageBean();
			else if ( type == File_Type_Rule ) return new SendRuleBean();
			else {
				throw new Error("请传入正确对象类型");
				return null;
			}
		}
		/**
		 * 写入文件 
		 * @param type
		 * @param obj
		 */		
		public static function write( type:uint ,  array:ArrayCollection ):void {
			
			var len:uint = array.length;
			var bytes:ByteArray = new ByteArray();
			bytes.writeInt( len );
			for each( var item:Object in array ) {
				bytes.writeObject( item );
			}
			bytes.compress();
			
			var file:flash.filesystem.File = new flash.filesystem.File( getFileUrl( type ) );
			var fileStream:FileStream = new FileStream();
			fileStream.open( file , FileMode.WRITE );
			fileStream.writeBytes( bytes );
			fileStream.close();
		}
		
		/**
		 * 读取的数据类型 
		 * @param type
		 */		
		public static function read( type:uint ):ArrayCollection {
			var file:flash.filesystem.File = new flash.filesystem.File( getFileUrl( type ) );
			var arr:ArrayCollection = new ArrayCollection();
			if( !file.exists ) return arr;
			
			
			var byte:ByteArray = new ByteArray();
			var fileStream:FileStream = new FileStream();
			fileStream.open( file , FileMode.READ );
			fileStream.readBytes( byte );
			fileStream.close();
			
			byte.uncompress();
			byte.position = 0;
			
			var len:int = byte.readInt();
			for( var i:int = 0 ; i < len ; i++ ) {
				var desObj:Object = getObjClass( type );
				var item:Object = byte.readObject();
				Utils.copyObject( item , desObj);
				arr.addItem( desObj);
			}
			
			return arr;
		}
		
	}
}